#ifndef __MY_UTIL__
#define __MY_UTIL__

#include <iostream>
#include <fstream>
#include <sstream>
#include <memory>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <jsoncpp/json/json.h>

#include "Log.hpp"

using namespace log_ns;

namespace aod
{
    class FileUtil
    {
    private:
        std::string _name; // 文件名
    public:
        FileUtil(const std::string &name)
            : _name(name)
        {
        }

        // 查看文件是否存在
        bool Exists()
        {
            // access的F_OK专门用于检测文件是否存在--- 存在则返回0
            int ret = access(_name.c_str(), F_OK);
            if (ret != 0)
            {
                LOG(ERROR, "file is not exists!\n");
                return false;
            }
            return true;
        }
        // 返回文件大小
        size_t FileSize()
        {
            if (this->Exists() == false)
            {
                return 0;
            }

            struct stat st;
            // stat接口用于获取文件属性，其中 st_size就是文件大小成员
            int ret = stat(_name.c_str(), &st);
            if (ret != 0)
            {
                LOG(ERROR, "get file stat failed!\n");
                return 0;
            }
            return st.st_size;
        }

        // 读取文件数据到content中
        bool GetContent(std::string *content)
        {
            std::ifstream ifs;
            ifs.open(_name, std::ios::binary);
            if (ifs.is_open() == false)
            {
                LOG(ERROR, "open file failed!\n");
                return false;
            }
            size_t flen = this->FileSize();
            content->resize(flen);
            ifs.read(&(*content)[0], flen);
            if (ifs.good() == false)
            {
                LOG(ERROR, "read file content failed!\n");
                ifs.close();
                return false;
            }
            ifs.close();
            return true;
        }

        // 向文件写入数据
        bool SetContent(const std::string content)
        {
            std::ofstream ofs;
            ofs.open(_name, std::ios::binary);
            if (ofs.is_open() == false)
            {
                LOG(ERROR, "open file failed!\n");
                return false;
            }
            ofs.write(content.c_str(), content.size());
            if (ofs.good() == false)
            {
                LOG(ERROR, "write file content failed!\n");
                ofs.close();
                return false;
            }
            ofs.close();
            return true;
        }
        // 创建目录
        bool CreateDirectory()
        {
            if (this->Exists() == true)
            {
                return true;
            }
            mkdir(_name.c_str(), 0777);
            return true;
        }
    };

    class JsonUtil
    {
    public:
        // 序列化
        static bool Serialize(const Json::Value &value, std::string *str)
        {
            Json::StreamWriterBuilder swb;
            std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());

            std::stringstream ss;
            int ret = sw->write(value, &ss);
            if (ret != 0)
            {
                LOG(ERROR, "Serialize failed!\n");
                return false;
            }
            *str = ss.str();
            return true;
        }

        // 反序列化
        static bool UnSerialize(const std::string &str, Json::Value *value)
        {
            Json::CharReaderBuilder crb;
            std::unique_ptr<Json::CharReader> cr(crb.newCharReader());

            std::string error;
            int ret = cr->parse(str.c_str(), str.c_str() + str.size(), value, &error);
            if (ret == false)
            {
                LOG(ERROR, "UnSerialize failed!\n(errno info : %s)\n", errno);
                return false;
            }
            return true;
        }
    };
}

#endif